/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipo.mm.web.util.pager;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import org.ipo.mm.ejb.entities.Institution;
import org.ipo.mm.ejb.facade.institution.InstitutionFacadeLocal;

/**
 *
 * @author makcro
 */
public class InstitutionPager extends AbstractPager{
    private final String nameFilter;
    private final String nameOperator;
    private final String addressFilter;
    private final String addressOperator;
    private final String emailFilter;
    private final String emailOperator;
    private final String telephoneFilter;
    private final String telephoneOperator;
    private final String faxFilter;
    private final String faxOperator;
    private final String representativeFilter;
    private final String representativeOperator;
    private final String[] typeFilter;
    private final String[] regionFilter;
    private final String[] familyFilter;
    private final String[] cityFilter;
    private final String orderBy;

    public InstitutionPager(

            String nameFilter,
            String nameOperator,
            String addressFilter,
            String addressOperator,
            String emailFilter,
            String emailOperator,
            String telephoneFilter,
            String telephoneOperator,
            String faxFilter,
            String faxOperator,
            String representativeFilter,
            String representativeOperator,
            String[] typeFilter,
            String[] regionFilter,
            String[] familyFilter,
            String[] cityFilter,
            String orderBy

            ) {
        this.nameFilter = nameFilter;
        this.nameOperator = nameOperator;
        this.addressFilter = addressFilter;
        this.addressOperator = addressOperator;
        this.emailFilter = emailFilter;
        this.emailOperator = emailOperator;
        this.telephoneFilter = telephoneFilter;
        this.telephoneOperator = telephoneOperator;
        this.faxFilter = faxFilter;
        this.faxOperator = faxOperator;
        this.representativeFilter = representativeFilter;
        this.representativeOperator = representativeOperator;
        this.typeFilter = typeFilter;
        this.regionFilter = regionFilter;
        this.familyFilter = familyFilter;
        this.cityFilter = cityFilter;
        this.orderBy = orderBy;

        this.resultsTotal = lookupInstitutionFacade().getSearchResultCount(
                nameFilter,
                nameOperator,
                addressFilter,
                addressOperator,
                emailFilter,
                emailOperator,
                telephoneFilter,
                telephoneOperator,
                faxFilter,
                faxOperator,
                representativeFilter,
                representativeOperator,
                typeFilter,
                regionFilter,
                familyFilter,
                cityFilter,
                orderBy,
                false);
    }






    @Override
    protected void injectData(HttpServletRequest request) {

        Collection<Institution> institutions = lookupInstitutionFacade().search(
                nameFilter,
                nameOperator,
                addressFilter,
                addressOperator,
                emailFilter,
                emailOperator,
                telephoneFilter,
                telephoneOperator,
                faxFilter,
                faxOperator,
                representativeFilter,
                representativeOperator,
                typeFilter,
                regionFilter,
                familyFilter,
                cityFilter,
                orderBy,
                firstResult,
                maxResultsPerPage,
                false);

        request.setAttribute("INST_SEARCH_RESULTS", institutions);
        request.setAttribute("INST_SEARCH_PAGENUMBERS", generatePageNumbers());
        request.setAttribute("INST_SEARCH_TOTAL_RESULTS", this.resultsTotal);
        request.setAttribute("INST_SEARCH_SHOW_NEXT", new Boolean(this.showNext));
        request.setAttribute("INST_SEARCH_SHOW_PREV", new Boolean(this.showPrev));

    }

    @Override
    protected void setPageSize() {
        maxResultsPerPage = 999999;
    }


    private InstitutionFacadeLocal lookupInstitutionFacade() {
        try {
            Context c = new InitialContext();
            return (InstitutionFacadeLocal) c.lookup("java:comp/env/InstitutionFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
